<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/20
 * Time: 10:02
 */
namespace Com;

class Message{
    private static $APIURL = 'www.qybor.com:8500/shortMessage';

    private static $_config = array(
        'username'       =>  'juzhenbao',
        'passwd'          =>  'Seosite1952',
        'phone'        =>  '',
        'msg'           =>  '',
        'needstatus'    =>  true,
        'port'       =>  '',
        'sendtime'         =>  ''
    );

    /**
     * 定制属性设置
     * @param string $name  设置名称
     * @param string $value 设置值
     */
    public static function setConfig($name, $value){
        if(isset(self::$_config[$name])) {
            self::$_config[$name] = $value;
        }
    }

    /**
     * 组装短信模板并发送
     * @return string
     */
    public static function send($mobile,$msg,$param = array()){
        //TODO: 初始化

        self::setConfig('phone', $mobile);
        self::setConfig('msg', $msg.'【聚真宝】');//签名为必填项

        $result = self::curl_http(self::$APIURL, self::$_config);
        //return $result;
        return self::getError($result);
    }

    /**
     * 根据状态码返回错误
     * @param string $json
     * @return array
     */
    private function getError($feedback_data){
        $result=implode(',', $feedback_data);

        if (empty($feedback_data)) {
            $return['status']=0;
            $return['info'] = '请求失败，未知错误';
            return $return;
        }
        $code = $result[1];
        switch ($code) {
            case 0:
                $return['status'] = 1;
                $return['info'] = '请求成功';
                break;
            case 101:
                $return['status']=0;
                $return['info']='短信接口错误，无此用户';
                break;
            case 102:
                $return['status']=0;
                $return['info']='短信接口错误，密码不正确';
                break;
            case 103:
                $return['status']=0;
                $return['info']='号码过多（非审核短信单批小于5000，审核短信单批小于50000）';
                break;
            case 104:
                $return['status']=0;
                $return['info']='短信内容过长，单条内容最多支持536';
                break;
            case 105:
                $return['status']=0;
                $return['info']='短信接口账号余额不足';
                break;
            case 106:
                $return['status']=0;
                $return['info']='未带签名或签名位置错误，签名必须放在末尾';
                break;
            case 108:
                $return['status']=0;
                $return['info']='通道签名匹配错误';
                break;
            case 109:
                $return['status']=0;
                $return['info']='短信内容含有关键字';
                break;
            case 111:
                $return['status']=0;
                $return['info']='正则模板匹配错误';
                break;
            default:
                $return['status']=0;
                $return['info'] = '请求失败，请联系技术员查询';
                break;
        }

        return $return;
    }

    /** curl 获取 https 请求
     * @param string $url 请求的url
     * @param array $data 要發送的數據
     * @param array $header 请求时发送的header
     * @param int $timeout 超时时间，默认30s
     */
    private function curl_https($url, $data=array(), $header=array(), $timeout=30){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

        $response = curl_exec($ch);

        curl_close($ch);

        return $response;
    }

    /** curl 获取 http 请求
     * @param string $url 请求的url
     * @param array $data 要發送的數據
     * @param array $header 请求时发送的header
     * @param int $timeout 超时时间，默认30s
     */
    private function curl_http($url, $data=array(), $header=array(), $timeout=30){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

        $response = curl_exec($ch);

        curl_close($ch);

        return $response;
    }
}
